package com.order.api.service.impl;

import com.order.api.constant.BaseException;
import com.order.api.constant.MenuNodeEnum;
import com.order.api.constant.MsgEnum;
import com.order.api.dao.TblMenuDao;
import com.order.api.po.TblMenu;
import com.order.api.po.TblMenuExample;
import com.order.api.service.TblMenuService;
import com.order.api.util.ThreadLocalUtil;
import com.order.api.vo.request.MenuIdRequest;
import com.order.api.vo.request.MenuSaveRequest;
import com.order.api.vo.request.MenuStateRequest;
import com.order.api.vo.response.MenuListResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TblMenuServiceImpl implements TblMenuService {

    @Autowired
    private TblMenuDao tblMenuDao;

    @Autowired
    private ThreadLocalUtil threadLocalUtil;


    @Override
    public boolean saveMenu(MenuSaveRequest request) {
        boolean flag = request.getId() == null;
        if (StringUtils.isEmpty(request.getMenuName())) {
            throw new BaseException(MsgEnum.ERROR_MENU_NOT_NAME);
        }
        if (StringUtils.isEmpty(String.valueOf(request.getOrderNo()))) {
            throw new BaseException(MsgEnum.ERROR_MENU_NOT_NO);
        }
        if (StringUtils.isEmpty(request.getUrl())) {
            throw new BaseException(MsgEnum.ERROR_MENU_NOT_URL);
        }
        Long userId = threadLocalUtil.getUserId();
        long aLong = Long.parseLong(String.valueOf(userId));
        Date time = Calendar.getInstance().getTime();
        TblMenu tblMenu = TblMenu.builder()
                .id(request.getId())
                .menuName(request.getMenuName())
                .orderNo(request.getOrderNo())
                .parentId(request.getParentId())
                .status(request.getStatus())
                .url(request.getUrl())
                .createId(flag ? aLong : null)
                .createTime(flag ? time : null)
                .updateId(flag ? null : aLong)
                .updateTime(flag ? null : time)
                .build();
        int count = flag ? tblMenuDao.insertSelective(tblMenu) : tblMenuDao.updateByPrimaryKeySelective(tblMenu);
        return count == 1;
    }

    @Override
    public boolean deleteMenu(MenuIdRequest request) {
        return request.getId() != null && tblMenuDao.deleteByPrimaryKey(request.getId()) == 1;
    }

    @Override
    public TblMenu detailMenu(MenuIdRequest request) {
        return request.getId() != null ? tblMenuDao.selectByPrimaryKey(request.getId()) : null;
    }

    @Override
    public boolean updateState(MenuStateRequest request) {
        int count = tblMenuDao.updateByPrimaryKeySelective(TblMenu.builder()
                .id(request.getId())
                .status(request.getStatus())
                .build());
        return count == 1;
    }

    @Override
    public List<MenuListResponse> findList() {
        List<MenuListResponse> menuList = tblMenuDao.selectByExample(new TblMenuExample()).stream()
                .map(x -> MenuListResponse.builder()
                        .id(x.getId())
                        .menuName(x.getMenuName())
                        .orderNo(x.getOrderNo())
                        .parentId(x.getParentId())
                        .url(x.getUrl())
                        .status(x.getStatus())
                        .build())
                .collect(Collectors.toList());
        if (menuList.size() > 0) {
            menuList = MenuListResponse.buildTree(menuList, MenuNodeEnum.ROOT_NODE_CODE.getCode());
        }
        return menuList;
    }

}
